﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Web.UI.HtmlControls;

namespace IntelligentCMS
{
    public partial class CMSDataGrid : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            
            
        }

        

        private DataTable data = new DataTable();

        private List<CMSColumnType> cmscolumntypes = new List<CMSColumnType>();

        public DataTable Data
        {

            get
            {
                return data;
            }

            set
            {
                data = value;

            }
        }

        public List<CMSColumnType> CMSColumnTypes
        {
            get
            {
                return cmscolumntypes;
            }
            set
            {
                cmscolumntypes = value;
            }


        }


        public void Execute()
        {
            //data.Clear();

            //theGridView.DataSource = new DataTable();

            
            //theGridView.refre


            /*
            DataTable newTable = new DataTable();

            newTable.Columns.Add("test", typeof(string));
            newTable.Columns.Add("peanut", typeof(string));


            DataRow newRow = newTable.NewRow();
            newRow["test"] = "423423423432";
            newRow["peanut"] = "peanut";

            newTable.Rows.Add(newRow);
            */
            theGridView.Columns.Clear();
            for (int i = 0; i < data.Columns.Count; i++)
            {
                switch (cmscolumntypes[i].Type)
                {
                    case CMSColumnType.CMSCOLUMNTYPE_Text:

                        BoundField boundColumn = new BoundField();
                        boundColumn.HeaderText = data.Columns[i].ColumnName;
                        boundColumn.DataField = data.Columns[i].ColumnName;
                        theGridView.Columns.Add(boundColumn);

                        break;

                    case CMSColumnType.CMSCOLUMNTYPE_Hyperlink:
                        HyperLinkField linkColumn = new HyperLinkField();
                        linkColumn.HeaderText = data.Columns[i].ColumnName;
                        linkColumn.DataTextField = data.Columns[i].ColumnName;
                       
                        linkColumn.DataNavigateUrlFields = new string[] { data.Columns[i].ColumnName };
                        linkColumn.DataNavigateUrlFormatString = cmscolumntypes[i].Data; //"~/MyPage.aspx?Param1={0}";
                        theGridView.Columns.Add(linkColumn);

                        break;
                    case CMSColumnType.CMSCOLUMNTYPE_Button:
                        ButtonField buttonColumn = new ButtonField();
                        buttonColumn.HeaderText = data.Columns[i].ColumnName;
                        buttonColumn.DataTextField = data.Columns[i].ColumnName;
                        buttonColumn.CommandName = cmscolumntypes[i].Data;
                        theGridView.Columns.Add(buttonColumn);
                        break;

                    default:

                        break;



                }

            }



            theGridView.AutoGenerateColumns = false;
            theGridView.DataSource = data;
            theGridView.DataBind();
        }

    }

    public class CMSColumnType
    {
        public CMSColumnType(string type, string data, string name)
        {
            this.data = data;
            this.type = type;
            this.name = name;

        }

        public const string CMSCOLUMNTYPE_Hyperlink = "Hyperlink";
        public const string CMSCOLUMNTYPE_Text = "Text";
        public const string CMSCOLUMNTYPE_Button = "Button";


        public string Type
        {
            get
            {
                return type;
            }

        }

        public string Data
        {
            get
            {
                return data;
            }


        }

        public string Name
        {
            get
            {

                return name;
            }

        }


        private string data = string.Empty;
        private string type = string.Empty;
        private string name = string.Empty;



    }

    

    

}